package es.udc.fbellas.j2ee.tiendaweb.http.controller.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import es.udc.fbellas.j2ee.tiendaweb.http.controller.session.SessionManager;
import es.udc.fbellas.j2ee.tiendaweb.http.view.actionforms.UserProfileForm;
import es.udc.fbellas.j2ee.tiendaweb.model.direccionpostal.TO.DireccionPostalTO;
import es.udc.fbellas.j2ee.tiendaweb.model.tarjetabancaria.TO.TarjetaBancariaTO;
import es.udc.fbellas.j2ee.tiendaweb.model.usuario.TO.UsuarioProfileTO;
import es.udc.fbellas.j2ee.tiendaweb.model.usuariofacade.exceptions.IncorrectPasswordException;
import es.udc.fbellas.j2ee.tiendaweb.model.usuariofacade.util.PasswordEncrypter;
import es.udc.fbellas.j2ee.util.exceptions.DuplicateInstanceException;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;
    
public class RegisterUserAction extends DefaultAction {

    public ActionForward doExecute(ActionMapping mapping,
        ActionForm form, HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException, InternalErrorException {
            
        /* Get data. */
        UserProfileForm userProfileForm = (UserProfileForm) form;
        String loginName = userProfileForm.getLoginName();
        String clearPassword = userProfileForm.getPassword();
        UsuarioProfileTO usuarioProfileTO = new UsuarioProfileTO(userProfileForm.getLoginName(),
        	userProfileForm.getPassword(),
            userProfileForm.getFirstName(), userProfileForm.getSurname(),
            userProfileForm.getEmail(),
            0,
            userProfileForm.getNumTarjeta(),
            userProfileForm.getLanguage(),
            userProfileForm.getCountry());
        DireccionPostalTO direccionPostalTO = new DireccionPostalTO(0,userProfileForm.getNombreReceptor(), userProfileForm.getCalle(), userProfileForm.getNumero(),userProfileForm.getPuerta(), userProfileForm.getCodPostal());
        TarjetaBancariaTO tarjetaBancariaTO = new TarjetaBancariaTO(userProfileForm.getNumTarjeta(), java.sql.Date.valueOf(userProfileForm.getFechaExp()));                                                            
        
        /* Register user. */            
        ActionMessages errors = new ActionMessages();
          
        try {
            SessionManager.registerUser(request, loginName, clearPassword,
                usuarioProfileTO,direccionPostalTO,tarjetaBancariaTO);
            SessionManager.login(request, response, loginName, userProfileForm.getPassword(),
                    false);

        } catch (DuplicateInstanceException e) {
            errors.add("loginName",
                new ActionMessage("ErrorMessages.loginName.alreadyExists"));
        }                  
        catch (InstanceNotFoundException e) {
            errors.add("loginName", new ActionMessage(
                "ErrorMessages.loginName.notFound"));
        } 
        catch (IncorrectPasswordException e) {
            errors.add("password", new ActionMessage(
                "ErrorMessages.password.incorrect"));

        }
        /* Return ActionForward. */
        if (errors.isEmpty()) {
            return mapping.findForward("MainPage");
        } else {
            saveErrors(request, errors);            
            return new ActionForward(mapping.getInput());
        }
        
    }

}
